package ai.parts;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import ai.jobs.AIJob;
import ai.service.AIService;
import ai.utils.DateUtils;

public class SamplePart {

	@Inject
	private MPart part;
	@Inject
	private IEventBroker eventBroker;
	@Inject
	private AIService aiService;

	private StyledText styledText;

	@PostConstruct
	public void createComposite(Composite parent) {
		parent.setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(parent, SWT.BORDER);
		FormLayout formLayout = new FormLayout();
		formLayout.spacing = 10;
		formLayout.marginHeight = 10;
		formLayout.marginWidth = 10;
		composite.setLayout(formLayout);

		styledText = new StyledText(composite, SWT.BORDER | SWT.V_SCROLL | SWT.READ_ONLY | SWT.WRAP);
		styledText.setWordWrap(true);
		styledText.setText("你好，欢迎提问！");
		FormData fd_styledText = new FormData();
		fd_styledText.top = new FormAttachment(0, 0);
		fd_styledText.left = new FormAttachment(15, 0);
		fd_styledText.right = new FormAttachment(85, 0);
		fd_styledText.bottom = new FormAttachment(80, 0);
		styledText.setLayoutData(fd_styledText);

		Label label = new Label(composite, SWT.NONE);
		label.setText("回答区：");
		FormData fd_Label = new FormData();
		fd_Label.top = new FormAttachment(styledText, 0, SWT.TOP);
		fd_Label.right = new FormAttachment(styledText, 0, SWT.LEFT);
		label.setLayoutData(fd_Label);

		StyledText styledTextQuestion = new StyledText(composite, SWT.BORDER | SWT.V_SCROLL | SWT.WRAP);
		styledTextQuestion.setWordWrap(true);
		FormData fd_styledTextQuestion = new FormData();
		fd_styledTextQuestion.top = new FormAttachment(styledText, 0, SWT.BOTTOM);
		fd_styledTextQuestion.left = new FormAttachment(15, 0);
		fd_styledTextQuestion.right = new FormAttachment(85, 0);
		fd_styledTextQuestion.bottom = new FormAttachment(100, 0);
		styledTextQuestion.setLayoutData(fd_styledTextQuestion);

		Label labelQuestion = new Label(composite, SWT.NONE);
		labelQuestion.setText("提问区：");
		FormData fd_LabelQuestion = new FormData();
		fd_LabelQuestion.top = new FormAttachment(styledTextQuestion, 0, SWT.TOP);
		fd_LabelQuestion.right = new FormAttachment(styledTextQuestion, 0, SWT.LEFT);
		labelQuestion.setLayoutData(fd_LabelQuestion);

		Button buttonQuestion = new Button(composite, SWT.NONE);
		buttonQuestion.setText("发送");
		FormData fd_ButtonQuestion = new FormData();
		fd_ButtonQuestion.bottom = new FormAttachment(styledTextQuestion, 0, SWT.BOTTOM);
		fd_ButtonQuestion.left = new FormAttachment(styledTextQuestion, 0, SWT.RIGHT);
		buttonQuestion.setLayoutData(fd_ButtonQuestion);
		buttonQuestion.setEnabled(false);

		styledTextQuestion.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				String questionContext = styledTextQuestion.getText();
				if (questionContext == null || questionContext.trim().length() == 0) {
					buttonQuestion.setEnabled(false);
				} else {
					buttonQuestion.setEnabled(true);
				}
			}

		});

		part.getContext().set(StyledText.class, styledText);

		buttonQuestion.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				String questionContext = styledTextQuestion.getText();
				styledTextQuestion.setText("");

				styledText.append("\r\n\r\n" + DateUtils.getNowStr1() + " 问题内容：" + questionContext);
				styledText.setTopIndex(styledText.getLineCount());
				AIJob aiJob = new AIJob("应答任务", questionContext, aiService, eventBroker);
				aiJob.schedule();
				buttonQuestion.setEnabled(false);
			}
		});
	}

	@Inject
	@Optional
	public void waiting(@UIEventTopic("ai/waiting") Object object) {
		styledText.append("\r\n 问题解析中请稍等...");
		styledText.setTopIndex(styledText.getLineCount());
	}

	@Inject
	@Optional
	public void addAnswerToText(@UIEventTopic("ai/addAnswerToText") String answer) {
		styledText.append("\r\n" + DateUtils.getNowStr1() + " 问题回答：" + answer);
		styledText.setTopIndex(styledText.getLineCount());
	}

	@Persist
	public void save() {
		part.setDirty(false);
	}
}